/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.zx.Logic;

import com.zx.ObjectClasses.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 *
 * @author VcLi
 */
public class TrainManager {

    private BufferedReader in;
    private Map<String, TrainService> trainServiceMap = new HashMap<String, TrainService>();
    //This is the arraylist to store stn name with no duplication
    private Map<String, Station> stnMap = new HashMap<String, Station>();

    public TrainManager() {
        this.readFile();
    }

//    public static void main(String[] args) {
//        TrainManager tm = new TrainManager();
//        tm.retrieveStnMap();
//    }
    //Testing Done



    private void readFile() {
        TrainService ts = null;
        try {
            in = new BufferedReader(
                    new FileReader(new File("record/train.txt")));
            String dataLine;
            while ((dataLine = in.readLine()) != null) {
                //This regex can detect the starting line and ending line <end>
                if (dataLine.matches("<[^>]*>")) {
                    //if the string is not <end>
                    if (!dataLine.matches("<end>")) {
                        //get the content in <> 
                        String[] tokens = dataLine.split("<");
                        String[] tokens2 = tokens[1].split(">");

                        ts = new TrainService();
                        //tokens2[0] is the content within <>
                        ts.setName(tokens2[0]);
                        // key is the Train Service Name and value is the ts object
                        trainServiceMap.put(ts.getName(), ts);
//                        System.out.println(ts.getServiceName());
//                        System.out.println("--------------");
                    }
                } else {
                    //if it's not the starting line, then start loop through to create stations
                    this.genStnMap(dataLine, ts);
                }
            }
            in.close();

        } catch (FileNotFoundException ex) {
            System.out.println("File Not Found");
        } catch (IOException e) {
            System.out.println("Error,the file is empty");
        }
    }

    private void genStnMap(String dataLine, TrainService ts) {

        //create a new station and set the name of the station,
        //this is to store the stn and put into ts
        Station stn = null;
        boolean isDuplicatedStn = false;

        //operatingTsInStn.add(ts);
        Collection c = stnMap.values();
        Iterator itr = c.iterator();

        //retrieve from Map
        while (itr.hasNext()) {
            //if the station already in the map, use the existing one
            stn = (Station) itr.next();
            if (stn.getName().trim().equals(dataLine.trim())) {
                //determine whether the station is an interchange in the readfile method
                stn.setInterchange(true);
                isDuplicatedStn = true;
                stn.getOperatingTsInStn().add(ts);
                break;
            }
        }
        //Not in Map , need to create a new one.
        if (!isDuplicatedStn) {
            stn = new Station();
            stn.setName(dataLine);
            stn.getOperatingTsInStn().add(ts);
            stnMap.put(stn.getName(), stn);
        }

        //To generate the ts arraylist for a station
        //put the station into the current running ts object
        ts.getStnList().add(stn);
    }

    public Map<String, Station> getStnMap() {
        return stnMap;
    }

    public Map<String, TrainService> getTrainServiceMap() {
        return trainServiceMap;
    }
}
